﻿using MemberManagement.Common;
using MemberManagement.Context;
using MemberManagement.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MemberManagement
{
    public partial class SysConfigForm : Form
    {
        public SysConfigForm()
        {
            InitializeComponent();
        }

        private void SysConfigForm_Load(object sender, EventArgs e)
        {
            GetSysConfig();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtPwd.Text))
            {
                MessageBox.Show("密码不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtPwd.Focus();
                return;
            }
            ((Button)sender).Enabled = false;
            if (this.UpdateSystemConfig())
            {
                MessageBox.Show("参数修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            ((Button)sender).Enabled = true;
        }

        #region 私有函数

        private bool UpdateSystemConfig()
        {
            try
            {
                using (var db = new MyContext())
                {
                    //金额
                    var amount = db.SysConfig.FirstOrDefault(o => o.Key == ConstKey.AmountKey);
                    if (amount == null)
                    {
                        db.SysConfig.Add(new Entity.SysConfig
                        {
                            Key = ConstKey.AmountKey,
                            Value = this.amount.Value.ToString()
                        });
                    }
                    else
                    {
                        amount.Value = this.amount.Value.ToString();
                    }
                    //密码
                    var pwd = db.SysConfig.FirstOrDefault(o => o.Key == ConstKey.PwdKey);
                    if (pwd == null) throw new Exception("缺少初始密码配置");
                    pwd.Value = this.txtPwd.Text.DESEncrypt();
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "操作数据失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }


        private bool GetSysConfig()
        {
            try
            {
                using (var db = new MyContext())
                {
                    var configList = db.SysConfig.ToList();
                    var amount = configList.FirstOrDefault(c => c.Key.ToLower() == ConstKey.AmountKey);
                    if (amount != null)
                    {
                        this.amount.Value = int.Parse(amount.Value);
                    }
                    var pwd = configList.FirstOrDefault(c => c.Key == ConstKey.PwdKey);
                    if (pwd != null && !string.IsNullOrEmpty(pwd.Value))
                    {
                        this.txtPwd.Text = pwd.Value.DESDecrypt();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "操作数据失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }
        #endregion
    }
}
